package logs

import (
	"github.com/sirupsen/logrus"
	"gopkg.in/natefinch/lumberjack.v2"
	"io"
	"os"
)

var Logger *logrus.Logger

type LoggerConfig struct {
	FileName string
	MaxSize  int
	MaxAge   int
	Level    logrus.Level
}

// Init 初始化日志设置
func Init(l *LoggerConfig) {
	Logger = logrus.New()
	Logger.Formatter = &logrus.JSONFormatter{
		TimestampFormat: "2006-01-02 15:04:05.000",
	}
	Logger.Level = l.Level //logrus.TraceLevel
	Logger.SetReportCaller(true)
	dir, _ := os.Getwd()
	// 使用 MultiWriter 将日志输出到多个目标：文件和控制台
	logWriter := io.MultiWriter(os.Stdout, &lumberjack.Logger{
		Filename: dir + "\\" + l.FileName,
		MaxSize:  l.MaxSize,
		MaxAge:   l.MaxAge,
	})
	Logger.Out = logWriter
}
